/**
* ...
* @author Brandon Flowers
* @version 0.1
* @time 01/15/2009 5:57 PM 
*/

package packagename.states 
{
	// HIVE
	import org.hive.managers.plugin.LayoutManager;
	import org.hive.managers.plugin.LoadManager;
	import org.hive.managers.plugin.SoundManager;
	import org.hive.managers.StateManager;
	import org.hive.state.*
	import org.hive.sequence.*
	import org.hive.events.LoaderSetEvent;
	import org.hive.data.LoaderSet;
	import org.hive.data.SoundData;
	import org.hive.data.LoaderData;
		
	// FLASH
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.media.Sound;
	import flash.net.*;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.AntiAliasType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getDefinitionByName;
	
	// PROJECTNAME
	import packagename.data.ProjectNameContentList;
	import packagename.states.ProjectNameHelloWorldState;
	import packagename.views.PreloaderView;
	
	public class ProjectNamePreloadingState extends State implements IState  
	{
	
		private var loading									:PreloaderView;

		public override function create():ActionSequence
		{			
			trace ("ProjectNamePreloadingState / create()");
			
				
			contentList = contentList as ProjectNameContentList;
			
			// LOAD MANAGER
					
			var preloadSequence:ActionSequence = new ActionSequence( "Preloader Sequence" );
									
			LoadManager.add
			(
				// SWFS
				new LoaderData( "studio", contentList.getContentByName( 'library' )  ),
				new SoundData( "bleep", contentList.getContentByName( 'bleep' )  )
			);

			// LOADERSET
			var preloadLoaderSet:LoaderSet = new LoaderSet("Preload LoaderSet", 
															10,
															"studio",
															"bleep"
															);												
															
															
			LoadManager.add(preloadLoaderSet);	
			
			// EVENTS
			preloadLoaderSet.addEventListener( LoaderSetEvent.ON_CHANGE, contentLoadProgressHandler, false, 0, true );
			preloadLoaderSet.addEventListener( LoaderSetEvent.ON_COMPLETE, completeHandler, false, 0, true );
			
			// Sequence
			preloadSequence.push( preloadLoaderSet );
							
			// at this point, you probably want to create your own amazing loading movie clip or sprite
			// but for this sample, I'll use a simple movie clip with a text field	
			loading = new PreloaderView();

			loading.x = LayoutManager.stage.stageWidth / 2 - (loading.width / 2);
			loading.y = LayoutManager.stage.stageHeight / 2 - (loading.height / 2);
					
			preloadSequence.push( loading );
			
			LayoutManager.stage.addEventListener(Event.RESIZE, stageResizeHandler);
			LayoutManager.stage.dispatchEvent(new Event(Event.RESIZE));
			
			// RETURN			
			return preloadSequence;
		}
		
		private function stageResizeHandler(e:Event)
		{
			trace ("ProjectNamePreloadingState / resize");	
			loading.x = LayoutManager.stage.stageWidth / 2 - (loading.width / 2);
			loading.y = LayoutManager.stage.stageHeight / 2 - (loading.height / 2);
		}
	
			
		private function contentLoadProgressHandler( e:LoaderSetEvent ):void
		{
			//trace ("PreloaderState / contentLoadProgressHandler() / e.percent: " + e.percent);
			loading.feedPreloader(e.percent);
		}
		 		 
		private function playSoundSequence(soundName:String):void
        {	
			SoundManager.init();
			SoundManager.add( LoadManager.getLoaderByName( soundName ) );
									
			var soundSequence:ActionSequence = new ActionSequence("Sound Sequence: " + soundName);
						
			var soundFactory:SoundData = SoundManager.getSoundByName(soundName);
	 										  
		
			soundSequence.push( soundFactory  );
				
			StateManager.playSequence(soundSequence);
		}
				
		private function completeHandler(e:LoaderSetEvent=null )
		{
			trace ("ProjectNamePreloadingState / completeHandler");
			//
			playSoundSequence("bleep");
			//
			nextState("packagename.states.ProjectNameHelloWorldState");
		}
		
			
		// we need create a null instance of the next state(s) so 
		// that we could potentially move into different states 
		// depending on what happens in this state
		// but since we know the preloading state will always move into
		// the first state we only need to create ProjectNameFirstState
		
		private var nextStateInstance:ProjectNameHelloWorldState = null;
				
		private function nextState(stateClassName:String):void
		{
			var nextStateSequence:ActionSequence = new ActionSequence( "Next State " + stateClassName );
			var ClassReference:Class = getDefinitionByName(stateClassName) as Class;
           				
			nextStateSequence.push( new SequenceFunction(StateManager, StateManager.switchState, new ClassReference() ));
			StateManager.playSequence(nextStateSequence);
						
		}
		
		
		private function kill():void
		{
			trace ("ProjectNamePreloadingState / kill ");
			LayoutManager.removeChild(loading);
		}
		
		public override function close():CloseSequence
		{
			trace ("ProjectNamePreloadingState / close sequence" );
			var closeSequence:CloseSequence = new CloseSequence( "ProjectNamePreloadingState::closeSequence" );
			closeSequence.push( {scope:loading, alpha:0, time:1} );
			closeSequence.push( new SequenceFunction(this, kill) );
			
			return closeSequence;
			
		}
		
	}
	
}
